﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using DBConnect;

namespace TISS_Add_in
{
    public partial class uc_SelectGuiaType : UserControl
    {
        private string selectedFile;

        public uc_SelectGuiaType()
        {
           
                InitializeComponent();
                btnOk.Enabled = false;
                saveFileDialog1.Filter = "Microsoft Office Word 2007 Document (*.docx)|*.docx";
                ImageList imgLst = new ImageList();
                imgLst.ImageSize = new Size(60, 60);
                imgLst.Images.Add(Image.FromHbitmap(Properties.Resources.icon_word07.GetHbitmap()));
                listViewGuias.LargeImageList = imgLst;                     
            
        }

        private void btnOk_Click(object sender, EventArgs e)
        {            
            if (listViewGuias.SelectedItems[0] != null)
            {
                ListViewItem selectedItem = listViewGuias.SelectedItems[0];
                selectedFile = "";
                string FileName = "guia" + listViewGuias.SelectedItems.ToString() + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss");
                selectedFile = textBoxSelectedFile.Text;
                string templatePath = "";
                switch (selectedItem.Index)
                {
                    case 0:
                        templatePath = AppDomain.CurrentDomain.BaseDirectory + "templates\\guiaConsulta.docx";
                        break;
                    case 1:
                        templatePath = AppDomain.CurrentDomain.BaseDirectory + "templates\\guiaHonorarioIndividual.docx";
                        break;
                    case 2:
                        templatePath = AppDomain.CurrentDomain.BaseDirectory + "templates\\guiaOdontologia.docx";
                        break;                     
                    case 3:
                        templatePath = AppDomain.CurrentDomain.BaseDirectory + "templates\\guiaResumoInternacao.docx";
                        break;                    
                    case 4:
                        templatePath = AppDomain.CurrentDomain.BaseDirectory + "templates\\guiaSP_SADT.docx";
                        break;
                    case 5:
                        templatePath = AppDomain.CurrentDomain.BaseDirectory + "templates\\guiaSolicitacaoProrrogacao.docx";
                        break;
                    case 6:
                        templatePath = AppDomain.CurrentDomain.BaseDirectory + "templates\\guiaSolicitacaoOdonto.docx";
                        break;
                    case 7:
                        templatePath = AppDomain.CurrentDomain.BaseDirectory + "templates\\guiaSolicitacaoInternacao.docx";
                        break;
                    case 8:
                        templatePath = AppDomain.CurrentDomain.BaseDirectory + "templates\\guiaSolicitacaoSP_SADT.docx";
                        break; 
                    case 9:
                        templatePath = AppDomain.CurrentDomain.BaseDirectory + "templates\\guiaConsultaReapresentacao.docx";
                        break;
                    case 10:
                        templatePath = AppDomain.CurrentDomain.BaseDirectory + "templates\\guiaHonorarioIndividualReapresentacao.docx";
                        break;
                    case 11:
                        templatePath = AppDomain.CurrentDomain.BaseDirectory + "templates\\guiaOdontologiaReapresentacao.docx";
                        break;
                    case 12:
                        templatePath = AppDomain.CurrentDomain.BaseDirectory + "templates\\guiaResumoInternacaoReapresentacao.docx";
                        break;
                    case 13:
                        templatePath = AppDomain.CurrentDomain.BaseDirectory + "templates\\guiaSP_SADTReapresentacao.docx";
                        break;
                }
                if (templatePath != "")
                {
                    File.Copy(templatePath, selectedFile, true);
                    AddinUtils.AbrirGuia(selectedFile);

                    Globals.ThisAddIn.doc = Globals.ThisAddIn.Application.ActiveDocument;
                    AddinUtils.AlteraLogoGuia(null);

                    uc_NumeroGuia ucNumeroGuia = new uc_NumeroGuia();
                    ucNumeroGuia.NodeContents.regANS = "";
                    ucNumeroGuia.NodeContents.tipo_guia = (TipoGuia)selectedItem.Index;
                    ucNumeroGuia.numeroGuiaChanged += new uc_NumeroGuia.numeroGuiaChangedHandler(this.ucNumeroGuia_numeroGuiaChanged);

                    Globals.ThisAddIn.NumeroGuiaCTP = Globals.ThisAddIn.CustomTaskPanes.Add(ucNumeroGuia, "Número da Guia");
                    Globals.ThisAddIn.NumeroGuiaCTP.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating;
                    Globals.ThisAddIn.NumeroGuiaCTP.Height = 118;
                    Globals.ThisAddIn.NumeroGuiaCTP.Width = 180;
                    Globals.ThisAddIn.NumeroGuiaCTP.Visible = true;
                    Globals.ThisAddIn.NumeroGuiaCTP.Control.Focus();
                    Globals.ThisAddIn.NumeroGuiaCTP.VisibleChanged += new EventHandler(NumeroGuiaCTP_VisibleChanged);

                    Globals.ThisAddIn.CustomTaskPanes.Remove(Globals.ThisAddIn.SelectGuiaTypeCTP);
                }
               
            }
        }

        void NumeroGuiaCTP_VisibleChanged(object sender, EventArgs e)
        {
            if (!Globals.ThisAddIn.NumeroGuiaCTP.Visible)
            {
                try
                {
                    object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                    object mis = Globals.ThisAddIn._missing;
                    Globals.ThisAddIn.Application.ActiveDocument.Close(ref doNotSaveChanges, ref mis, ref mis);
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }

        public void ucNumeroGuia_numeroGuiaChanged(object sender)
        {
            uc_NumeroGuia ucNumeroGuia = (uc_NumeroGuia)sender;

            if (ucNumeroGuia.NodeContents.numeroGuia != "")
            {
                object numeroGuiaCC = Globals.ThisAddIn.IndexNumeroGuiaCC;
                if (CommandGuias.verificaNumeroGuia(Globals.ThisAddIn.UsuarioID, ucNumeroGuia.NodeContents.regANS, ucNumeroGuia.NodeContents.numeroGuia) == 1)
                    MessageBox.Show("Este número de guia já existe","Alert");
                else
                    Globals.ThisAddIn.doc.ContentControls.get_Item(ref numeroGuiaCC).Range.Text = ucNumeroGuia.NodeContents.numeroGuia;
            }
        }

        private void buttonSelectLocation_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            bool itemSelecionado = false;
            if (listViewGuias.SelectedItems[0] != null)
            {
                ListViewItem selectedItem = listViewGuias.SelectedItems[0];
                switch (selectedItem.Index)
                {
                    //guia consulta
                    case 0:
                        itemSelecionado = true;
                    break;
                    //guia honorario individual
                    case 1:
                       itemSelecionado = true; 
                    break;
                    //guia odontologia
                    case 2:
                        itemSelecionado = true; 
                    break;
                    //guia resumo internacao
                    case 3:
                        itemSelecionado = true;
                    break;
                    //guia SP_SADT
                    case 4:
                        itemSelecionado = true;
                    break;
                    //guia Solicitacao Prorrogacao
                    case 5:
                        itemSelecionado = true;
                    break;
                    //guia Solicitacao odonto
                    case 6:
                        itemSelecionado = true;
                    break;
                    //guia Solicitacao ResumoInternacao
                    case 7:
                        itemSelecionado = true;
                    break;
                    //guia Solicitacao SP_SADT
                    case 8:
                        itemSelecionado = true;
                    break;
                    //guia Consulta reapresentacao
                    case 9:
                        itemSelecionado = true;
                    break;
                    //guia Honorario individual reapresentacao
                    case 10:
                        itemSelecionado = true;
                    break;
                    //guia Odontologia reapresentacao
                    case 11:
                        itemSelecionado = true;                       
                    break;
                    //guia Resumo internacao reapresntacao
                    case 12:
                        itemSelecionado = true;
                    break;
                    //guia SP_SADT reapresentacao
                    case 13:
                        itemSelecionado = true;
                    break;
                    
                     
                }
                if (itemSelecionado == true)
                {
                    textBoxSelectedFile.Text = saveFileDialog1.FileName;
                    selectedFile = textBoxSelectedFile.Text;
                    btnOk.Enabled = true;
                }
            }
        }

        private void listViewGuias_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewGuias.SelectedItems.Count > 0)
                groupBox2.Enabled = true;
            else
                groupBox2.Enabled = false;
        }

    
    }
}
